<?php

class Core_Component_System_Controller_Backend_Dms_Property {
    
    public function __construct() {
        $h = $GLOBALS[RC]->getHelper();
        $h->setLocation('{system.system}', '{system.dms}', '{system.label.dms}');
        if($GLOBALS[RC]->getRequest()->hasParameter('entityid')){
            $h->setLocation(App::loadModel('system/dms_entity', $GLOBALS[RC]->getRequest()->getParameter('entityid'))
                ->getClassName());
        }
    }

    public function indexAction($entityid) {
        $GLOBALS[RC]->getHelper()->setLocation('{system.label.property_list}');
        $list = App::getModel('system/dms_property')
                    ->getQuery()
                    ->condition('entity_id', $entityid)
                    ->orderby('sort', 'asc')
                    ->fetch();
        return $GLOBALS[RC]->getHelper()->templateResult(array('list' => $list, 'entity_id'=>$entityid));
    }
    
    public function createAction($entityid = NULL) {
        $GLOBALS[RC]->getHelper()->setLocation('{system.label.create_property}');
        return $GLOBALS[RC]->getHelper()->templateResult(array('entity_id'=>$entityid), '*/*/edit');
    }    

    public function editAction($id) {
        $model = App::loadModel('system/dms_property', $id);
        $GLOBALS[RC]->getHelper()->setLocation($model->getName());
        return $GLOBALS[RC]->getHelper()->templateResult(array('entity_id'=>$model->getEntityId(), 'model'=>$model), '*/*/edit');
    }

    public function saveAction(Core_Component_System_Model_Dms_Property $model) {
        $isNew = $model->merge()->isNew();
        $model->validate()->save();
        $req = $GLOBALS[RC]->getRequest();
        $bus = App::getBusiness('system/localize');
        $labelText = $req->getParameter('label_text', NULL);
        $descText = $req->getParameter('desc_select', NULL);
        if($labelText){
            $bus->writeLanguageFile($model->getLabel(), $labelText);
        }
        if($descText){
            $bus->writeLanguageFile($model->getLabel(), $labelText);
        }

        return $GLOBALS[RC]->getHelper()->historyResult('*/*/*/index');
    }
    
    public function deleteAction($id) {
        $model = App::loadModel('system/dms_property', $id);
        App::getBusiness('system/dms_entity')->removeField($model);
        $model->delete();
        return $GLOBALS[RC]->getHelper()->historyResult('*/*/*/index');
    }
    
    public function changeVersionAction($id){
        $model = App::loadModel('system/dms_property', $id);
        App::getBusiness('system/dms_entity')->changeVersionField($model);
        $GLOBALS[RC]->getMessager()->addSuccess('{system.message.success}');
        return $GLOBALS[RC]->getHelper()->historyResult('*/*/*/index');        
    }
    
    public function sortAction($entityid){
        $GLOBALS[RC]->getHelper()->setLocation('{system.label.property_sort}');
        $list = App::getModel('system/dms_property')
                    ->getQuery()
                    ->condition('entity_id', $entityid)
                    ->orderby('sort', 'asc')
                    ->fetch();
        return $GLOBALS[RC]->getHelper()->templateResult(array('list'=>$list->toArray()));
    }
    
    public function sortPostAction($ids){
        foreach($ids as $index=>$id){
            App::getModel('system/dms_property')
                    ->loadBy(array('id'=>$id))
                    ->setSort($index + 1)
                    ->save();
        }
        $GLOBALS[RC]->getMessager()->addSuccess('{system.message.success}');
        return $GLOBALS[RC]->getHelper()->historyResult('*/*/*/index');         
    }    
}
